package org.jeecg.modules.cw.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.cw.entity.*;
import org.jeecg.modules.cw.mapper.CwXmzcsrbMapper;
import org.jeecg.modules.cw.mapper.CwXmzcsrbTjMapper;
import org.jeecg.modules.cw.service.ICwJbszqkzbService;
import org.jeecg.modules.cw.service.ICwSzzbService;
import org.jeecg.modules.cw.service.ICwXmzcsrbService;
import org.jeecg.modules.cw.service.ICwXmzcsrbTjService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 项目支出收入支出提交表
 * @Author: jeecg-boot
 * @Date:   2025-09-16
 * @Version: V1.0
 */
@Service
public class CwXmzcsrbTjServiceImpl extends ServiceImpl<CwXmzcsrbTjMapper, CwXmzcsrbTj> implements ICwXmzcsrbTjService {
	@Autowired
	private ICwSzzbService cwSzzbService;
	@Autowired
	private CwXmzcsrbTjMapper cwXmzcsrbTjMapper;
	@Autowired
	private CwXmzcsrbMapper cwXmzcsrbMapper;
	@Autowired
	private ICwJbszqkzbService cwJbszqkzbService;
	@Autowired
	private ISysDepartService sysDepartService;
	@Autowired
	private ISysBaseAPI iSysBaseAPI;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CwXmzcsrbTj cwXmzcsrbTj, List<CwXmzcsrb> cwXmzcsrbList) {

		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String departIdsByOrgCode = iSysBaseAPI.getDepartIdsByOrgCode(user.getOrgCode());
		SysDepart byId = sysDepartService.getById(departIdsByOrgCode);
		String departName = byId.getDepartName();
		cwXmzcsrbTj.setUnit(departName);
		Double a=0.0;
		Double b=0.0;
		Double c=0.0;
		cwXmzcsrbTjMapper.insert(cwXmzcsrbTj);
		if (cwXmzcsrbList != null && cwXmzcsrbList.size() > 0) {
			for (CwXmzcsrb entity : cwXmzcsrbList) {
				//外键设置
				Double undertakingRevenue = entity.getUndertakingRevenue();
				Double otherRevenue = entity.getOtherRevenue();
				Double fczbkjy = entity.getFczbkjy();
				if (undertakingRevenue==null){
					undertakingRevenue=0.0;
				}else if (otherRevenue==null){
					otherRevenue=0.0;
				}else if (fczbkjy==null){
					fczbkjy=0.0;
				}
				a+= undertakingRevenue;
				b+=otherRevenue;
				c+=fczbkjy;
				entity.setUnit(cwXmzcsrbTj.getUnit());
				entity.setMainId(cwXmzcsrbTj.getId());
				cwXmzcsrbMapper.insert(entity);
			}
		}

	}
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CwXmzcsrbTj cwXmzcsrbTj, List<CwXmzcsrb> cwXmzcsrbList) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String departIdsByOrgCode = iSysBaseAPI.getDepartIdsByOrgCode(user.getOrgCode());
		SysDepart byId = sysDepartService.getById(departIdsByOrgCode);
		String departName = byId.getDepartName();
		String id = user.getId();
		cwXmzcsrbTjMapper.updateById(cwXmzcsrbTj);
		//1.先删除子表数据
		cwXmzcsrbMapper.deleteByMainId(cwXmzcsrbTj.getId());
		Double a=0.0;
		Double b=0.0;
		Double c=0.0;
		//2.子表数据重新插入
		if(cwXmzcsrbList!=null && cwXmzcsrbList.size()>0) {
			for(CwXmzcsrb entity:cwXmzcsrbList) {
				//外键设置
				Double undertakingRevenue = entity.getUndertakingRevenue();
				Double otherRevenue = entity.getOtherRevenue();
				Double fczbkjy = entity.getFczbkjy();
				if (undertakingRevenue==null){
					undertakingRevenue=0.0;
				}else if (otherRevenue==null){
					otherRevenue=0.0;
				}else if (fczbkjy==null){
					fczbkjy=0.0;
				}
				a+= undertakingRevenue;
				b+=otherRevenue;
				c+=fczbkjy;
				entity.setMainId(cwXmzcsrbTj.getId());
				cwXmzcsrbMapper.insert(entity);
			}
		}
//		LambdaQueryWrapper<CwJbszqkzb> wrapper = new LambdaQueryWrapper<>();
//		wrapper.eq(CwJbszqkzb::getYear,cwXmzcsrbTj.getYear())
//				.eq(CwJbszqkzb::getUnit,departName);
//		Double sysr=0.0;
//		//其他收入
//		Double qtsr=0.0;
//		//使用非财政拨款结余
//		Double fczbk=0.0;
//		for (CwJbszqkzb cwJbszqkzb : cwJbszqkzbService.list(wrapper)) {
//			if ("事业收入".equals(cwJbszqkzb.getProject())){
//				Double ysshj = cwJbszqkzb.getYsshj();
//				if (ysshj==null){
//					ysshj=0.0;
//				}
//				sysr=cwJbszqkzb.getYsshj();
//			}
//			if ("其他收入".equals(cwJbszqkzb.getProject())){
//				Double ysshj = cwJbszqkzb.getYsshj();
//				if (ysshj==null){
//					ysshj=0.0;
//				}
//				qtsr=cwJbszqkzb.getYsshj();
//			}
//			if ("使用非财政拨款结余".equals(cwJbszqkzb.getProject())){
//				Double ysshj = cwJbszqkzb.getYsshj();
//				if (ysshj==null){
//					ysshj=0.0;
//				}
//				fczbk=cwJbszqkzb.getYsshj();
//			}
//		}
//		LambdaQueryWrapper<CwSzzb> cwSzzbLambdaQueryWrapper = new LambdaQueryWrapper<>();
//		cwSzzbLambdaQueryWrapper.eq(CwSzzb::getYear,cwXmzcsrbTj.getYear())
//				.eq(CwSzzb::getUnit,departName);
//
//		long count = cwSzzbService.count(cwSzzbLambdaQueryWrapper);
//		if (count==1){
//			CwSzzb one = cwSzzbService.getOne(cwSzzbLambdaQueryWrapper);
//			one.setSysr(a+sysr);
//			one.setQtsr(b+qtsr);
//			one.setFc(c+fczbk);
//			one.setYear(cwXmzcsrbTj.getYear());
//			one.setUserId(id);
//			cwSzzbService.updateById(one);
//		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		cwXmzcsrbMapper.deleteByMainId(id);
		cwXmzcsrbTjMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {

		for (Serializable id : idList) {


			cwXmzcsrbMapper.deleteByMainId(id.toString());
			cwXmzcsrbTjMapper.deleteById(id);


		}
	}

}
